<template>
  <!-- 管理员布局 -->
  <div v-if="isAdmin" class="admin-layout">
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside width="200px" class="admin-sidebar">
        <div class="logo"><img src="../assets/logo.jpg" alt="Logo" width=200 height=auto /> </div>
        <el-menu
            router
            :default-active="$route.path"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
        >
          <el-menu-item index="/manage/user">
            <i class="el-icon-user"></i>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/manage/canteen">
            <i class="el-icon-food"></i>
            <span>餐厅管理</span>
          </el-menu-item>
          <el-menu-item index="/order">
            <i class="el-icon-food"></i>
            <span>订单中心</span>
          </el-menu-item>
          <el-menu-item index="/community">
            <i class="el-icon-food"></i>
            <span>社区管理</span>
          </el-menu-item>
          <el-menu-item index="/tasteHome">
            <i class="el-icon-house"></i>
            <span>返回首页</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 右侧内容区 -->
      <el-container>
        <el-header class="admin-header">
          <div class="admin-user-info">
            当前登录：
            <router-link v-if="isLoggedIn" to="/userInfo" class="username">{{ username }}</router-link>
<!--            <router-link to="/userInfo" class="username">{{ username }}</router-link>-->
<!--            <el-button type="text" @click="logout" style="margin-left: 10px;">退出</el-button>-->
          </div>
        </el-header>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>

  <!-- 普通用户布局 -->
  <el-header v-else class="header">
    <el-row type="flex" justify="space-between" align="center">
      <el-col>
        <el-menu mode="horizontal" router :default-active="$route.path">
          <div class="logo"><img src="../assets/logo.jpg" alt="Logo" width=150 height=auto /> </div>
          <el-menu-item index="/tasteHome" class="item">网站首页</el-menu-item>
          <el-menu-item index="/community" class="item">美食社区</el-menu-item>
          <el-menu-item index="/canteen" class="item">餐厅预约</el-menu-item>
          <el-menu-item index="/order" class="item">个人订单</el-menu-item>
        </el-menu>
        <div class="right-buttons">
          当前登录：
          <router-link v-if="isLoggedIn" to="/userInfo" class="username">{{ username }}</router-link>
          <router-link v-else to="/login" class="login-link">未登录</router-link>
        </div>
      </el-col>
    </el-row>
  </el-header>
</template>

<script>
export default {
  name: 'HeaderComponent',
  data() {
    return {
      isLoggedIn: false,
      username: '',
      userType: '',
      isAdmin: false
    };
  },
  mounted() {
    this.checkLoginStatus();
  },
  methods: {
    checkLoginStatus() {
      this.isLoggedIn = !!localStorage.getItem('token');
      this.username = localStorage.getItem('username') || '';
      this.userType = localStorage.getItem('type') || '';
      this.isAdmin = this.userType === '0'; // 假设 '0' 是管理员类型
    },
    logout() {
      localStorage.clear();
      this.checkLoginStatus();
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
/* 普通用户样式 */
.header {
  background-color: #43678e;
  padding: 0;
  margin: 0 10px;
}

.right-buttons {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.item {
  border: none;
}

.username {
  font-size: 16px;
  color: #333;
  margin-left: 10px;
  text-decoration: none;
}

.username:hover {
  color: #4a688a;
}

.login-link {
  font-size: 16px;
  color: #4a688a;
  text-decoration: none;
}

/* 管理员样式 */
.admin-layout {
  height: 100vh;
}

.admin-sidebar {
  background-color: #545c64;
  height: 100vh;
  color: white;
}

.admin-sidebar .logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
}

.admin-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 20px;
}

.admin-user-info {
  display: flex;
  align-items: center;
}

/* 图标样式 */
.el-icon-user,
.el-icon-food,
.el-icon-house {
  margin-right: 8px;
}
</style>
